package cn.cas.mango.util.paramCheck;

import cn.cas.mango.dto.param.QueryParam;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import java.util.Map;
import java.util.stream.Collectors;

import co.elastic.clients.elasticsearch._types.FieldValue;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;



@Slf4j
public class QueryParamCheckUtil {

  /**
   * 检查参数.
   */
  public static Map<String, Object> checkParam(QueryParam formData, int pageSize) {
    Map<String, Object> map = new HashMap<>();
    String isSuccess = ParamCheckUtil.checkParam(formData);
    formData.setIdsArray(new ArrayList<>());
    if (Strings.isNotBlank(formData.getIds())) {
      String ids = formData.getIds();
      formData.setIdsArray(Arrays.stream(ids.split(",")).map(FieldValue::of).collect(Collectors.toList()));
    }
    if (!isSuccess.equals("ok")) {
      map.put("isSuccess", isSuccess);
      return map;
    }
    if (formData.getPageSize() == null || formData.getPageSize() < 0) {
      formData.setPageSize(pageSize);
    }

    if (formData.getParam() == null || formData.getParam() < 0 || formData.getParam() > 1) {
      formData.setParam(0.5);
    }

    String queryField = formData.getQueryField();
    if (Strings.isBlank(queryField)) {
      formData.setQueryField("all");
    } else {
      if (!queryField.equals("all") && !queryField.equals("content") && !queryField.equals(
          "title")) {
        String info = "参数[queryField]非法，取值应为[all | title | content]";
        log.info(info);
        map.put("isSuccess", info);
        return map;
      }
    }
    //搜索词
    if (Strings.isBlank(formData.getQueryStr())) {
      formData.setQueryStr("");
    }
    //精准or模糊
    if (Strings.isBlank(formData.getSearchType())) {
      formData.setSearchType("true");
    } else {
      String searchType = formData.getSearchType();
      if (!(searchType.equalsIgnoreCase("true") || searchType.equalsIgnoreCase("false"))) {
        String info = "参数[searchType]非法，取值应为[true | false]";
        log.info(info);
        map.put("isSuccess", info);
        return map;
      }
    }
    if (Strings.isBlank(formData.getSortType())) {
      if (formData.getSearchType().equalsIgnoreCase("true")) {
        formData.setSortType("sim");
      } else {
        formData.setSortType("time");
      }
    } else {
      String sortType = formData.getSortType();
      if (!sortType.equals("time") && !sortType.equals("timeasc") && !sortType.equals("sim")) {
        String info = "参数[sortType]非法，取值应为[time | timeasc | sim]";
        log.error(info);
        map.put("isSuccess", info);
        return map;
      }
    }
    //type可为空
    String type = formData.getType();
    if (Strings.isNotBlank(type)) {
      if (!(type.equalsIgnoreCase("image") || type.equalsIgnoreCase("video"))) {
        String info = "参数[type]非法，取值应为[image | video]";
        log.error(info);
        map.put("isSuccess", info);
        return map;
      }
    } else {
      formData.setType("");
    }
    if (Strings.isBlank(formData.getLikeDegree())) {
      formData.setLikeDegree("0");
    }

    if (Strings.isBlank(formData.getRecommendType())) {
      formData.setRecommendType("keywords");
    }

    log.info("Request param:{}", formData);
    map.put("isSuccess", isSuccess);
    map.put("formData", formData);
    return map;
  }
}

